package com.wen.wenda.controller;

import com.wen.wenda.common.BaseCode;
import com.wen.wenda.common.BaseResponse;
import com.wen.wenda.common.utils.ReturnUtil;
import com.wen.wenda.exception.BusinessException;
import com.wen.wenda.model.domain.Question;
import com.wen.wenda.model.request.app.AppSearchRequest;
import com.wen.wenda.model.request.question.QuestionCreateRequest;
import com.wen.wenda.model.request.question.QuestionUpdateRequest;
import com.wen.wenda.model.vo.QuestionVO;
import com.wen.wenda.service.QuestionService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 题目接口
 *
 * @author wen
 */
@RestController
@RequestMapping("/question")
public class QuestionController {

    @Resource
    private QuestionService questionService;

    @PostMapping("/create")
    public BaseResponse<Boolean> createQuestion(@RequestBody QuestionCreateRequest questionCreateRequest, HttpServletRequest request) {
        // 判断参数是否为空
        if (questionCreateRequest == null || request == null) {
            throw new BusinessException(BaseCode.PARAMS_NULL_ERROR);
        }
        // todo 具体的添加逻辑
        Question question = new Question();
        BeanUtils.copyProperties(questionCreateRequest, question);
        boolean save = questionService.save(question);
        return ReturnUtil.success(save);
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteQuestion(@RequestBody long id, HttpServletRequest request) {
        if (request == null) {
            throw new BusinessException(BaseCode.PARAMS_NULL_ERROR);
        }
        boolean removedById = questionService.removeById(id);
        if (!removedById) {
            throw new BusinessException(BaseCode.RESOURCE_NOT_FOUND, "删除失败");
        }
        return ReturnUtil.success(true);
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> updateQuestion(@RequestBody QuestionUpdateRequest questionUpdateRequest, HttpServletRequest request) {
        // 判断参数是否为空
        if (questionUpdateRequest == null || request == null) {
            throw new BusinessException(BaseCode.PARAMS_NULL_ERROR);
        }
        // todo 具体的修改逻辑
        Question question = questionService.getById(questionUpdateRequest.getId());
        BeanUtils.copyProperties(questionUpdateRequest, question);
        boolean res = questionService.updateById(question);
        return ReturnUtil.success(res);
    }

    @GetMapping("/list")
    public BaseResponse<List<QuestionVO>> getQuestionList(AppSearchRequest appSearchRequest, HttpServletRequest request) {
        if (appSearchRequest == null || request == null) {
            throw new BusinessException(BaseCode.PARAMS_NULL_ERROR);
        }
        // todo 具体的查询校验逻辑
        List<Question> list = questionService.list();
        List<QuestionVO> questionVOList = list.stream().map(item -> {
            QuestionVO questionVO = new QuestionVO();
            BeanUtils.copyProperties(item, questionVO);
            return questionVO;
        }).toList();
        return ReturnUtil.success(questionVOList);
    }
}
